package com.tz.crm.listener;

import org.apache.log4j.Logger;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

/**
 * MyPathListener
 * 在项目启动时，将项目名存入application作用域
 */
@WebListener
public class MyPathListener implements ServletContextListener {

    private static final Logger LOGGER = Logger.getLogger(MyPathListener.class);

    /**
     * Servlet容器启动Web应用
     * 项目启动时,将项目名存入application作用域 key 为 path
     * @param servletContextEvent Servlet容器上下文事件
     */
    public void contextInitialized(ServletContextEvent servletContextEvent) {
        ServletContext application = servletContextEvent.getServletContext();
        String path = application.getContextPath();
        LOGGER.debug("set path:" + path + " to application scope.");
        application.setAttribute("path", path);
    }

    /**
     * Servlet容器终止Web应用
     * @param servletContextEvent Servlet容器上下文事件
     */
    public void contextDestroyed(ServletContextEvent servletContextEvent) {
    }
}
